package com.quik.web.room.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.quik.common.Constants;
import com.quik.dto.PaginationDto;
import com.quik.entity.Room;
import com.quik.entity.SysAccount;
import com.quik.exception.ErrorCode;
import com.quik.web.room.service.RoomService;
/**
 * 功能说明：
 *      房间管理的业务逻辑
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/room")
public class RoomController {
	
	private static Logger log = (Logger)Logger.getLogger(RoomController.class);
	
	@Autowired
	private RoomService roomService;
	
	/**
	 * 进入房间管理界面
	 * @return
	 */
	@RequestMapping(value = "roomList", method = RequestMethod.GET)
	public ModelAndView roomList(){
		ModelAndView mav = new ModelAndView("/room/roomList");
		mav.addObject("roomStatusFree", Room.ROOM_STATUS_FREE);
		return mav;
	}
	
	/**
	 * 根据条件查询房间列表
	 * @param page
	 * @param rows
	 * @param roomNo
	 * @param roomTypeName
	 * @param roomAreaName
	 * @return
	 */
	@RequestMapping(value = "queryRoom", method = RequestMethod.POST)
	@ResponseBody
	public PaginationDto queryRoom(Integer page, Integer rows, String roomNo, String roomTypeName, String roomAreaName){
		PaginationDto dto = new PaginationDto(0, null);
		
		try{
			int intPage = (page == null || page == 0) ? 1:page;  //当前页
		    int pageSize = (rows == null || rows == 0) ? Constants.TREE_PAGE_SIZE : rows;  //每页显示的记录
		    
		    int totalCount = roomService.findRoomCount(roomNo, roomTypeName, roomAreaName);
		    List list = roomService.findRoom(roomNo, roomTypeName, roomAreaName, intPage, pageSize);
		    
		    dto.setRows(list);
		    dto.setTotal(totalCount);
		}catch(Exception e){
			e.printStackTrace();
			log.error("RoomController queryRoom error: " + e.getMessage());
		}
		
		return dto;
	}
	
	/**
	 * 进入新增房间界面
	 * @return
	 */
	@RequestMapping(value = "initAddRoom")
	public ModelAndView initAddRoom(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/room/addRoom");
		String result = (String)request.getSession().getAttribute("result");
		
		if(result != null && result.length() > 0)
			mav.addObject("result", result);
		
		mav.addObject("roomStatusFree", Room.ROOM_STATUS_FREE);
		mav.addObject("roomStatusUsed", Room.ROOM_STATUS_USED);
		
		return mav;
	}
	
	/**
	 * 检查房间编号是否唯一
	 * @param roomNo
	 * @return
	 */
	@RequestMapping(value = "checkRoomNo", method = RequestMethod.POST)
	public ResponseEntity<String> checkRoomNo(String roomNo){
		boolean flag = false;
		
		try{
			flag = roomService.checkRoomNo(roomNo);
		}catch(Exception e){
			e.printStackTrace();
			log.error("RoomController checkRoomNo error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(flag), HttpStatus.OK);
	}
	
	/**
	 * 新增房间
	 * @param room
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addRoom", method = RequestMethod.POST)
	public ModelAndView addRoom(Room room, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/room/roomType/addRoomType");
		SysAccount account = (SysAccount)request.getSession().getAttribute(Constants.SESSION_ACCOUNT);
		String result = ErrorCode.RESULT_ADD_ROOM_FAILED;
		
		try{
			room.setCrt_user(account.getStaff_no());
			room.setCrt_date(new java.util.Date(System.currentTimeMillis()));
			
			roomService.addRoom(room);
			
			result = ErrorCode.RESULT_ADD_ROOM_SUCCESS;
			mav.setViewName("forward:/room/initAddRoom");
		}catch(Exception e){
			e.printStackTrace();
			log.error("RoomController addRoom error: " + e.getMessage());
			result = ErrorCode.SYSTEM_ERROR;
		}
		
		mav.addObject("result", result);
		
		return mav;
	}
}
